package com.longrise.android.byjk.plugins.tabfirst.allfunction;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by callxkj on 2017/3/8.
 * AllFunctionActivit的presenter
 */

public class AllFunctionPresenter extends AllFunctionContract.Presenter {


    @Override
    protected void init() {
         requestAllFunction();
    }

    @Override
    protected void refresh() {

    }

    /**
     * 获取全部课程
     */
    private void requestAllFunction(){
        //先加载缓存本地的数据
        EntityBean[] bbt_sAppAllMenuModule = CacheUtils.getCacheFirstData("bbt_sAppAllMenuModule");
        if(bbt_sAppAllMenuModule!=null){
            parseAllFunction(bbt_sAppAllMenuModule);
        }

        String userid = UserInfor.getInstance().getUserid();
        String usersfzh = UserInfor.getInstance().getUsersfzh();
        EntityBean bean = new EntityBean();
        bean.set("apptype", "android");
        bean.set("userid", userid);
        bean.set("cardno", usersfzh);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_sAppAllMenuModule",
                fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        EntityBean[] resultBeans = bean.getBeans("result");
                        parseAllFunction(resultBeans);
                        CacheUtils.cacheFirstData("bbt_sAppAllMenuModule", resultBeans);
                    } else {
                        mView.showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

            }
        });
    }

    /**
     *解析全部功能
     */
    private void parseAllFunction(EntityBean[] beans){
         mView.refreshData(beans);
    }
}
